<?php 
session_start();
include "../_init_.php";
include(constant('PATH_INCLUDE').'/class.uploadhelper.php');

$dtReplyLog = new DataTable(constant('DB_TABLE_CONTACT_REPLY'));

$action= isset($_REQUEST["action"])?$_REQUEST["action"]:'';
switch($action)
{
	case 'send':
	$mailto = $_REQUEST['to'];
	$subject = $_REQUEST['subject'];
	$content = $_REQUEST['content'];
	$from = $_REQUEST['from'];
	//send out mail
	if(strlen($from) > 0)
		$headers= "From:$from\r\n";
	//$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
	mail($mailto, $subject, $content, $headers);
	
	//save to database
	$newData = array(
	'contact_id'=>$_REQUEST['contactid'],
	'to'=>$mailto,
	'subject'=>$subject,
	'content'=>$content,
	'from'=>$from,
	'create_datetime'=>date('Y-m-d H:i:s')
	);
	$dtReplyLog->save($newData);
	break;
}

$DT = new DataTable(constant('DB_TABLE_CONTACT_INFO'));
$info = $DT->get($_REQUEST['id']);

$condition = sprintf("`contact_id`=%d",$_REQUEST['id']);
$reply_log_list = $dtReplyLog->get_list($condition,'-create_datetime');
?>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站管理系統</title>


<LINK REL="stylesheet" HREF="css/style.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="css/main.css" TYPE="text/css">
<script src="js/FancyZoom.js" language="JavaScript" type="text/javascript"></script>
<script src="js/FancyZoomHTML.js" language="JavaScript" type="text/javascript"></script>
<script src="../js/jquery-ui-1.7.3.custom/js/jquery-1.3.2.min.js" language="javascript"></script>
<script language="javascript">
$(function(){
	$('#btnReply').click(function(){
		$('#replyinputpanel').toggle();
	});
});
</script>
<body onLoad="setupZoom();">
<table width="98%" border="0" class="table2">
  <tr>
    <td><a href="admin_contactus_list.php">查看聯絡我們</a></td>
  </tr>
</table>
<?php if($error_message):?>
<div class="error_message"><?php echo $error_message;?></div>
<?php endif;?>
<?php if($sucess_message):?>
<div class="success_message"><?php echo $sucess_message;?></div>
<?php endif;?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td width="100%" align="center" valign="top">
      <table  width="100%" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr class="tr_southidc"> 
            <td bgcolor="#B0DAFF"><table width="100%" border="0" align="center" cellspacing="1" >
                <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">姓名：</div></td>
                  <td width="87%" bgcolor="#FFFFFF">
                  <?php echo $info['textfield'];?>
                  </td>
                </tr>
                <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">所在地區：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield2'];?></td>
                </tr>			
                <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">地址：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield3'];?></td>
                </tr>
                 <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">郵遞區號：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield4'];?></td>
                </tr>
                 <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">E-mail：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield5'];?></td>
                </tr>
                 <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">行動電話：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield6'];?></td>
                </tr>
                 <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">電話：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield7'];?></td>
                </tr>
                 <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">傳真：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield8'];?></td>
                </tr>
                 <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">詢問的問題：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><?php echo $info['textfield9'];?></td>
                </tr>
                <tr>
                	<td colspan="2" align="center"><input type="button" value="回覆" id="btnReply"/></td>
                </tr>
            </table></td>
        
        </tr>
      </table>
    </td>
</tr>
</table>
<br>
<form method="post">
<input type="hidden" name="action" value="send"/>
<input type="hidden" name="contactid" value="<?php echo $info['id'];?>"/>
<table width="98%" border="0" class="table2" id="replyinputpanel" style="display:none;">
  <tr>
    <td>發送到:</td>
    <td><input type="text" name="to" value="<?php echo $info['textfield5'];?>"/></td>
  </tr>
  <tr>
  	<td>標題：</td>
  	<td><input type="text" name="subject" value=""/></td>
  </tr>
  <tr>
  	<td>內容：</td>
  	<td>
  	<textarea cols="100" rows="10" name="content"></textarea>
  	</td>
  </tr>
 <tr>
  	<td>From：</td>
  	<td><input type="text" name="from" value=""/></td>
  </tr>
<tr>
    <td colspan="2" align="center"><input type="submit" value="確認送出"/></td>
</tr>
</table>
</form>

<?php if(count($reply_log_list) > 0):?>
<table width="98%" border="0" class="table2">
  <tr>
    <td>發送歷史</td>
  </tr>
  <?php foreach($reply_log_list as $row):?>
  <tr>
  	<td>
  		<table border="0" class="table2" id="replyinputpanel" style="border:none;">
  		  <tr>
  	<td>發送時間：</td>
  	<td><?php echo $row['create_datetime'];?></td>
  </tr>
  <tr>
    <td>發送到:</td>
    <td><?php echo $row['to'];?></td>
  </tr>
  <tr>
  	<td>標題：</td>
  	<td><?php echo $row['subject'];?></td>
  </tr>
  <tr>
  	<td>內容：</td>
  	<td>
  	<?php echo nl2br($row['content']);?>
  	</td>
  </tr>
 <tr>
  	<td>From：</td>
  	<td><?php echo $row['from'];?></td>
  </tr>
 
  		</table>
  	</td>
  </tr>
  <tr>
  	<td>&nbsp;</td>
  </tr>
  <?php endforeach;?>
</table>
<?php endif;?>

</body>
</html>